Java methods are a set of instructions designed to execute a particular task efficiently. method offer a easy way to modify code as needed. Within this segment, we will deep dive into the concept of Java methods, explore various types of methods, understand their utilization, and the process of invoking them in Java.So let's start this journey
// Java program using class and object
class Student{ public static void main(String args[]){ System.out.print("The maximum number is: " + Math.sqrt(64)); } }
Output:
8
// Java program using class and object
class Student{ public static void main(String args[]){ int x = 10; int y = 20; int z = addition(x, y); //Here x and y are actual parameters & add method is called System.out.println("The sum of x and y is = " + z); } public static int addition(int a, int b) //a and b are formal parameters // Here Method is static so that we can access without object { int sum; sum=a+b; return sum; //returning the sum } }
Output:
The sum of x and y is = 30
In the above example, we have used add method it is not already define so we have define it first and access in main method, we have made add method static because we can access static method without object. you wil learn about static method below this page
A method name typically consists of one or more words first name of method should be a lowercase letter and use a verb. If the method name consists of more than two words then first name should be start with a verb and followed with an adjective or noun.
Programmers can define multiple methods with the same name but different parameters thanks to method overloading. Based on the quantity, nature, and arrangement of the parameters, Java can distinguish between them. This implies that as long as their parameter lists vary, we can have multiple methods with the same name.This function offers flexibility and improves the readability of the code.
// Java program using class and object
class Addition{ static int add(int x,int y){ return x+y; } static int add(int p,int q,int r){ return p+q+q; } } class Student{ public static void main(String args[]){ System.out.println(Addition.add(10,20)); System.out.println(Addition.add(10,20,30)); } }
Output:
30
50
A static method in Java is associated with the class itself, not with any particular instance of the class. It can be used even without producing a class object. This makes it convenient for utility methods or operations that don't need instance-specific data because you can access the method directly using the class name.
The primary benefit of static methods is their capacity to offer shared functionality for all instances of a class. You can guarantee that any object or method in the same class, or even in other classes, can access and use a method by declaring it static. This encourages code organisation and reuse.
// Java program using class and object
class Student{ public static void main(String args[]){ add(10, 20); } static void add(int x,int y){ System.out.println(x+y); } }
Output:
30
The term "abstract method" refers to a method without a method body. In other words, an abstract method is one that does not have an implementation. The abstract class is where it always declares. If a class has an abstract method, then the class itself must also be abstract. We use the keyword abstract to create an abstract method.
Syntax
abstract void method_name();
// Java program using class and object
abstract class Exam //abstract class { abstract void displayMarks(); //abstract method declaration } public class Student extends Exam { void displayMarks() //method implementation { System.out.println("My First Abstract method"); } public static void main(String args[]) { Exam obj = new Student(); //creating object of abstract class obj.displayMarks(); //invoking abstract method } }
Output:
My First Abstract method
It is a method that sends an object back to its class of origin. Factory methods are all static methods.
An instance method in Java is a type of method that belongs to a specific object or instance of a class.Instance method is fundamental unit of object-oriented programming in Java.Instance methods are linked to paticular objects, unlike to static methods, which are connected to the class as a whole. A class's objects can each have their own set of instance variables and the ability to use instance methods to manipulate those variables.
As part of the class definition, Java requires you to define an instance method. The access modifier, return type (void if the method returns null), method name, optional parameters enclosed in parentheses, and the method body enclosed in curly braces make up the method declaration.
Syntax
abstract void method_name();
// Java program using class and object
abstract class Exam //abstract class { abstract void displayMarks(); //abstract method declaration } public class Student extends Exam { void displayMarks() //method implementation { System.out.println("My First Abstract method"); } public static void main(String args[]) { Exam obj = new Student(); //creating object of abstract class obj.displayMarks(); //invoking abstract method } }
Output:
My First Abstract method
Accessor Method:The accessor method is the name given to the method(s) that read the instance variable(s). The word "get" is prefixed to the method, making it simple to recognise. Getters is another name for it. It gives back the private field's value. It is used to obtain the private field's value.
Example
public int getName()
{
return name;
}
// Java program using class and object
public class Student { private static int roll=102; public static int getRoll() //accessor method { return roll; } public static void main(String[] args) { System.out.println(getRoll()); } }
Output:
102
Mutator Method:The method(s) both read and modify the values of the instance variable(s). The word "set" is prefixed to the method, making it simple to recognise. Modifiers or setters are other names for it. It gives no results back. It accepts a field-dependent parameter of the same data type. The private field's value is set using it.
Example
public int setId(int Id)
{
this .Id = Id;
}
// Java program using class and object
public class Student { private int roll=102; public void setRoll(int roll) //mutator method { this.roll = roll; } public void displayRoll() { System.out.println(roll); } public static void main(String[] args) { Student objStudent=new Student(); objStudent.displayRoll(); } }
Output:
102
Post your comment